package cn.itcast.order.service;

import cn.itcast.order.client.UserClient;
import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import cn.itcast.order.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private UserClient userClient;

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);
        //2.远程查询user
        //2.1url地址
//        String url="http://localhost:8081/user/"+order.getUserId();
        // 修改访问的url路径，用服务名代替ip、端口：
//        String url="http://userservice/user/"+order.getUserId();
        //2.2发起调用
//        User user=restTemplate.getForObject(url,User.class);


        //2利用Feign发起http请求，查询用户
        User user=userClient.findById(order.getUserId());


        //封装user存入order
        order.setUser(user);
        // 4.返回
        return order;
    }
}
